<template>
  <el-button @click="getCustomColumns();drawer=true">自定义列</el-button>
  <!-- <div class="tool-icon" :style="{color: drawer===true?'#409EFF':'#e5e7eb'}" @click="getCustomColumns();drawer=true">
    <el-icon>
      <Icon icon="ep:menu" :color="drawer===true?'#409EFF':'#909399'" :size="18" />
    </el-icon>
  </div> -->
  <el-drawer class="custom-columns-drawer" v-model="drawer" :size="'20%'" :direction="'rtl'" :with-header="false"
    :destroy-on-close="true">
    <header>
      <span>自定义列</span>
      <el-tooltip placement="top" content="关闭">
        <Icon class="close" icon="ep:close" :size="20" @click="handleClose" />
      </el-tooltip>
    </header>
    <main>
      <el-checkbox v-model="checkAll" :indeterminate="isIndeterminate" @change="handleCheckAllChange">
        全选
      </el-checkbox>
      <div class="columns-box">
        <VueDraggable v-model="customList" :animation="150" ghostClass="ghost" class="tool-container">
          <div v-for="column in customList" :key="column.field" class="item">
            <el-checkbox class="item-check" v-model="column.isShow" @change="handleCheckChange">
              <el-tooltip placement="top" :content="column.title">
                {{ column.title }}
              </el-tooltip>
            </el-checkbox>
            <span>
              <el-tooltip placement="top" content="冻结在左侧">
                <img :src="isFixed(column.fixed, 'left')" alt="" @click="fixedChange(column, 'left')">
              </el-tooltip>
              <el-tooltip placement="top" content="冻结在右侧">
                <img :src="isFixed(column.fixed, 'right')" alt="" @click="fixedChange(column, 'right')">
              </el-tooltip>
            </span>
          </div>
        </VueDraggable>
      </div>
    </main>
    <footer>
      <el-button @click="recoverDefault">恢复默认</el-button>
      <el-button type="primary" @click="saveFn">保存</el-button>
    </footer>
  </el-drawer>
</template>
<script lang="ts" setup>
  import { cloneDeep } from 'lodash'
  import { VueDraggable } from 'vue-draggable-plus'
  import * as WaybillApi from '@/api/queue/waybill'

  const emit = defineEmits(['columnsChange'])

  const props = defineProps({
    menuType: {
      type: String,
      default: '',
      required: true
    }
  })

  const drawer = ref(false)
  const checkAll = ref(false)
  const isIndeterminate = ref(false)
  const customList = ref([])

  const isFixed = computed(() => {
    return (value, pisition) => {
      if (value === pisition) {
        return new URL('@/assets/imgs/active-fixed.png', import.meta.url).href
      }
      return new URL('@/assets/imgs/fixed.png', import.meta.url).href
    }
  })

  const handleClose = () => {
    drawer.value = false
  }

  const handleCheckChange = () => {
    if (isCheckedAll(true)) {
      checkAll.value = true
      isIndeterminate.value = false
      return;
    }
    if (isCheckedAll(false)) {
      checkAll.value = false
      isIndeterminate.value = false
      return;
    }
    checkAll.value = false
    isIndeterminate.value = true
  }

  const isCheckedAll = (isShow) => {
    return customList.value.every(e => e.isShow === isShow)
  }

  const handleCheckAllChange = (value) => {
    customList.value = customList.value.map(e => {
      e.isShow = value
      return e
    })
    handleCheckChange()
  }

  const fixedChange = (column, position) => {
    if (column.fixed === position) {
      column.fixed = null
      return
    }
    column.fixed = position
  }

  const recoverDefault = () => {
    ElMessageBox.confirm('确认恢复默认？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'info'
    }).then(() => {
      saveCustomColumns('recover')
    })
  }

  const saveFn = () => {
    if (!isCheckedForColumns()) {
      ElNotification({
        title: '警告',
        message: '请至少选择一项',
        type: 'warning',
      })
      return;
    }
    saveCustomColumns()
  }

  const isCheckedForColumns = () => {
    return !!customList.value.filter(e => e.isShow).length
  }

  const saveCustomColumns = async (type) => {
    const data = { menuType: props.menuType }
    if (type === 'recover') {
      data['restoreDefault'] = 1
    } else {
      data['fieldAttributeList'] = customList.value
    }
    const res = await WaybillApi.saveWaybillColumns(data)
    if (res) {
      emit('columnsChange')
      handleClose()
    }
  }

  const getCustomColumns = async () => {
    // 源代码，别删
    // const res = await WaybillApi.getWaybillColumns({
    //   menuType: props.menuType
    // })
    // customList.value = res.fieldAttributeList || []
    // 临时代码
    customList.value = [
      {
        title: '编号',
        field: 'index',
        width: '65px',
        fixed: null,
        isShow: true,
        sortable: false,
        filterable: false,
        customText: true
      },
      {
        title: '所属企业',
        field: 'waybillStatus',
        width: '150px',
        fixed: null,
        isShow: true,
        sortable: false,
        filterable: false,
        customText: false
      },
      {
        title: '计划编号',
        field: 'goodsSettlementStatus',
        width: '150px',
        fixed: null,
        isShow: true,
        sortable: false,
        filterable: false,
        customText: false
      },
      {
        title: '状态',
        field: 'planConfig',
        width: '100px',
        fixed: null,
        isShow: true,
        sortable: false,
        filterable: false,
        customText: true
      },
      {
        title: '客户企业',
        field: 'verificationStatus',
        width: '150px',
        fixed: null,
        isShow: true,
        sortable: false,
        filterable: false,
        customText: false
      },
      {
        title: '产品',
        field: 'purchaserName',
        width: '150px',
        fixed: null,
        isShow: true,
        sortable: false,
        filterable: false,
        customText: true
      },
      {
        title: '交易总量',
        field: 'supplierName',
        width: '130px',
        fixed: null,
        isShow: true,
        sortable: false,
        filterable: false,
        customText: true
      },
      {
        title: '价格',
        field: 'coalSourceName',
        width: '140px',
        fixed: null,
        isShow: true,
        sortable: false,
        filterable: false,
        customText: true
      },
      {
        title: '装货地址',
        field: 'coalSubtypeName',
        width: '140px',
        fixed: null,
        isShow: true,
        sortable: false,
        filterable: false,
        customText: false
      },
      {
        title: '卸货地址',
        field: 'vehicleNo',
        width: '120px',
        fixed: null,
        isShow: true,
        sortable: false,
        filterable: false,
        customText: false
      },
      {
        title: '开始时间',
        field: 'driverName',
        width: '105px',
        fixed: null,
        isShow: true,
        sortable: false,
        filterable: false,
        customText: true
      },
      {
        title: '结束时间',
        field: 'driverMobile',
        width: '150px',
        fixed: null,
        isShow: true,
        sortable: false,
        filterable: false,
        customText: true
      },
      {
        title: '创建时间',
        field: 'loadingSiteName',
        width: '140px',
        fixed: null,
        isShow: true,
        sortable: false,
        filterable: false,
        customText: false
      },
      {
        title: '所属组织',
        field: 'landingPlace',
        width: '105px',
        fixed: null,
        isShow: true,
        sortable: false,
        filterable: false,
        customText: false
      },
      {
        title: '操作',
        field: 'operation',
        width: '200px',
        fixed: null,
        isShow: true,
        sortable: false,
        filterable: false,
        customText: true
      },
    ]
    handleCheckChange()
  }
</script>

<style lang="scss" scoped>
  .tool-icon {
    width: 30px;
    height: 30px;
    line-height: 34px;
    border-radius: 25px;
    cursor: pointer;
    text-align: center;
    border: 1px solid #e5e7eb;

    :hover {
      color: #409EFF;
    }
  }

  header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
    color: #72767b;
    padding-right: 20px;

    >span {
      font-weight: 600;
    }

    >i {
      cursor: pointer;
    }
  }

  main {
    height: calc(100% - 80px);

    .columns-box {
      height: calc(100% - 50px);
      overflow-y: auto;
    }
  }

  footer {
    display: flex;
    justify-content: flex-end;
    bottom: 20px;
    position: absolute;
    width: calc(100% - 40px);

    .el-button+.el-button {
      margin-left: 20px;
    }
  }

  .tool-container {
    width: 100%;
    padding: 10px;
    background-color: #FAFCFF;

    .item+.item {
      margin-top: 10px;
    }

    .item {
      height: 40px;
      cursor: move;
      padding: 0 8px;
      display: flex;
      align-items: center;
      background-color: #F2F6FC;

      .item-check {
        flex: 1;
        width: 0;
        cursor: move;
      }

      >span {
        display: flex;
        margin-left: 10px;

        img+img {
          margin-left: 4px;
        }

        img {
          width: 14px;
          height: 14px;
          cursor: pointer;
        }

        :nth-child(2) {
          transform: scaleX(-1);
        }
      }
    }
  }

  .close :hover {
    color: #409EFF;
  }

  .ghost {
    opacity: 0.5;
    background: #c8ebfb;
  }

  :deep(.el-checkbox__label) {
    width: 100%;
    display: block !important;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  :deep(.el-checkbox) {
    width: 100%;
  }

  :deep(.el-checkbox__label) {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
</style>
<style>
  .custom-columns-drawer .el-drawer__body {
    padding: 20px 0 20px 20px !important;
  }
</style>